import {Component, OnInit} from '@angular/core';
import {LoadingService} from '../../../shared/loading.service';
import {AlertService} from '../../../shared/alert.service';
import {UserService} from '../user.service';
import {ActivatedRoute} from "@angular/router";

@Component({
    selector: 'app-user-view',
    templateUrl: './user-view.component.html',
    styleUrls: ['./user-view.component.scss']
})
export class UserViewComponent implements OnInit {

    public dataList: any = [];
    public openid: any = '';

    constructor(public loading: LoadingService,
                public service: UserService,
                public alert: AlertService,
                public router: ActivatedRoute) {
    }

    ngOnInit() {
        this.router.params.subscribe(params => {
            this.openid = params.id;
        });
        this.getData();
    }

    // 获取列表数据
    getData() {
        this.loading.show();
        const params: any = {
            // token: localStorage.token,
            openid: this.openid

        };
        console.log(params);
        this.service.userDetail(params).subscribe(
            res => {
                console.log(res, '数据');
                if (res['code'] === 0) {
                    this.dataList = res['data'];
                    console.log(this.dataList['add_time'].substring(0, 10), '时间戳');
                    this.dataList['add_time'] = this.formatDateTime(this.dataList['add_time'].substring(0, 10) * 1000);
                    this.loading.hide();
                } else {
                    this.loading.hide();
                    this.alert.show(res['msg'])
                }
            },
            error => {
                this.loading.hide();
                this.alert.show('网络错误');
            }
        );
    }

    formatDateTime(inputTime) {
        const time = new Date(parseInt(inputTime));
        const y = time.getFullYear();
        const m = time.getMonth() + 1;
        const min = m < 10 ? ('0' + m) : m;
        const d = time.getDate();
        const day = d < 10 ? ('0' + d) : d;
        const h = time.getHours();
        const hours = h < 10 ? ('0' + h) : h;
        const minute = time.getMinutes();
        const second = time.getSeconds();
        const minutes = minute < 10 ? ('0' + minute) : minute;
        const seconds = second < 10 ? ('0' + second) : second;
        // return y + '.' + min + '.' + day;
        return y + '-' + min + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
    };


    back() {
        window.history.go(-1);
    }
}
